package net.expectx.gw.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.base.BaseConstants;
import net.expectx.common.base.BaseResultConstants;
import net.expectx.common.util.StringUtil;
import net.expectx.gw.bean.ContentArticle;
import net.expectx.gw.bean.ContentCategory;
import net.expectx.gw.service.ContentArticleService;
import net.expectx.gw.service.ContentCategoryService;
import net.expectx.gw.util.AdminConstants;
import net.expectx.gw.util.BaseResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * controller
 * @author lijian
 * @date 2019/1/18
 */
@Controller
@RequestMapping("/manager/content/article")
@Api(value = "控制器", description = "管理")
public class ContentArticleController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContentArticleController.class);
    @Autowired
    ContentArticleService contentArticleService;
    @Autowired
    ContentCategoryService contentCategoryService;
    @ApiOperation(value = "新闻主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(String type,ModelMap modelMap) {
        modelMap.put("type", type);
        return "news/index";
    }

    @ApiOperation(value = "新增新闻")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(ModelMap modelMap ,HttpServletRequest request,String type) {
        String strWhere = "1=1 and is_deleted=0 ";
        if(StringUtils.isNotBlank(type)){
            strWhere += "  and  type ='"+type+"'";
        }
        List<ContentCategory> contentCategories = contentCategoryService.findByAttr(strWhere, null);
        modelMap.put("category", contentCategories);
        modelMap.put("type", type);
        return "news/modify";
    }

    @ApiOperation(value = "修改新闻")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap ,String type) {
        String strWhere = "1=1 and is_deleted=0 ";
        if(StringUtils.isNotBlank(type)){
            strWhere += "  and  type ='"+type+"'";
        }
        ContentArticle contentArticle = contentArticleService.selectByPrimaryKey(id);
        List<ContentCategory> contentCategories = contentCategoryService.findByAttr(strWhere, null);
        modelMap.put("category", contentCategories);
        modelMap.put("contentArticle", contentArticle);
        modelMap.put("type", type);
        return "news/modify";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,String type
           ) {
        String strWhere = "1=1 and is_deleted=0 ";
        String strOrder = null;
        String name = request.getParameter("name");
        if(StringUtils.isNotBlank(type)){
            strWhere += " and category_id in(select id from content_category where type ='"+type+"')";

        }
        if (StringUtils.isNotBlank(name)) {
            strWhere += " and title like('%" + name + "%')";
        }
        List<ContentArticle> contentArticle = contentArticleService.findByPage(page, limit, strWhere, strOrder);
        long total = contentArticleService.count(strWhere);
        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", contentArticle);
        return result;
    }

    @ApiOperation(value = "新增新闻")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(ContentArticle contentArticle) {
        contentArticle.setGmtCreate(new Date());
        contentArticle.setGmtModified(new Date());
        contentArticle.setIsDeleted(AdminConstants.NO);
        return        contentArticleService.insertSelective(contentArticle);

    }

    /**
     * 对象添加方法
     */

    @ApiOperation(value = "修改新闻")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(ContentArticle contentArticle) {
        return   contentArticleService.updateByPrimaryKeySelective(contentArticle);

    }

    @ApiOperation(value = "删除新闻")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        Map<String, Object> map = new HashMap<>(2);
        int count = contentArticleService.logicDeletion(AdminConstants.YES, " `id`  in(" + ids + ")");
        map.put("flag", count > 0 ? true : false);
        return map;
    }
    @ApiOperation(value = "客户详情")
    @RequestMapping(value = "/v1/client", method = RequestMethod.GET)
    @ResponseBody
    public Object client(String type,String category) {
        Map<String, Object> map = new HashMap<>(2);
        String StrWhere=" 1=1";
        if(StringUtils.isNotBlank(category)){
            StrWhere += "  and category_id =" + category;
        }
        List<ContentArticle> clients = contentArticleService.findByAttr(StrWhere, null);
        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, clients), HttpStatus.OK);

    }
    @ApiOperation(value = "解决方案")
    @RequestMapping(value = "/v1/solution", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> solution() {
        Map<String, Object> map = new HashMap<>(2);
        List<ContentArticle> clients = contentArticleService.findByAttr("  category_id in( select id from content_category where type =3 )", null);

        return new ResponseEntity<>(new BaseResult(BaseResultConstants.SUCCESS, true, clients), HttpStatus.OK);

    }
}
